mod walker;

use ggez::{Context, GameResult, GameError, graphics};
use ggez::mint::Point2;
use walker::Walker;

pub struct State{
    walkers: Vec<walker::Walker>,
}

impl State{
    pub fn new() -> State {
        //Due to the fact that ggez redraws the canvas every time, I store the previous walker states in a vector. It is not necessary to do this, I just wanted to see a similar visualization.
        State{walkers : vec![]}
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, _ctx: &mut Context) -> GameResult {
        let mut walker: Walker;
        if self.walkers.is_empty() {
            walker = walker::Walker::new(Point2 { x: 200.0, y: 300.0 });
        } else {
            walker = walker::Walker::new(self.walkers.last().unwrap().get_location());
            let movement: walker::Movements = rand::random();
            walker.move_to(movement);
        }
        self.walkers.push(walker);
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);

        for walker in &self.walkers { 
            let location: Point2<f32> = walker.get_location();
            let rectangle = graphics::Mesh::new_rectangle(
                ctx,
                graphics::DrawMode::fill(),
                graphics::Rect::new(location.x, location.y, 1.0, 1.0),
                graphics::Color::BLACK,
            )?;
            canvas.draw(&rectangle, graphics::DrawParam::default());
        }

        canvas.finish(ctx)?;
        Ok(())
    }
}